package logic

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"net/http"
	"path"
	"runtime"
	"strconv"
	"strings"
	"tiktok/dao"
	"tiktok/models"
	"tiktok/web"
)

func FollowAction(ctx *gin.Context) {
	//获取当前文件名
	_, fullFilename, _, _ := runtime.Caller(0)
	fileName := path.Base(fullFilename)

	//在函数退出前，执行defer
	//捕捉异常后，程序不会异常退出
	defer func() {
		err := recover() //内置函数，可以捕捉到函数异常
		if err != nil {
			logrus.Error(fileName + GetFollowErr)
		}
	}()

	//这里的uid是被关注人的id,即传入参数中的to_user_id
	id := ctx.Query("to_user_id")
	token := ctx.Query("token")
	action := ctx.Query("action_type")
	db := dao.GetDB(ctx)

	//token解析
	user, err := web.ParseToken(token)
	if err != nil { //解析token失败
		logrus.Error(ParseTokenErr)
		ctx.JSON(http.StatusBadRequest,
			models.Status{
				StatusCode: -1,
				StatusMsg:  ServerErr,
			})
		return
	}
	fmt.Println(user)
	flag, err := strconv.ParseInt(action, 10, 64)
	if err != nil {
		ctx.JSON(http.StatusOK, models.Status{
			StatusCode: -1,
			StatusMsg:  ParseErr,
		})
	}

	//用户数据
	var userAction models.User
	db.Take(&userAction, id)

	//获取关注列表和关注数
	var followList UserList
	db.Table("tiktok_user").Select("follow_list").Where("uid = ?", user.Uid).Find(&followList.UserList)
	db.Table("tiktok_user").Select("follow_count").Where("uid = ?", user.Uid).Find(&followList.FollowCount)

	//获取被关注人粉丝列表
	var followerList UserList
	db.Table("tiktok_user").Select("follower_list").Where("uid = ?", id).Find(&followerList.UserList)
	db.Table("tiktok_user").Select("follower_count").Where("uid = ?", id).Find(&followerList.FollowerCount)

	if flag == 1 {
		userAction.IsFollow = true
		if strings.Contains(followList.UserList, id+"/") {
			ctx.JSON(http.StatusOK, models.Status{
				StatusCode: -1,
				StatusMsg:  FocusFailure,
			})
			return
		}

		followList.UserList += id + "/"
		followerList.UserList += strconv.FormatInt(user.Uid, 10) + "/"
		followList.FollowCount += 1
		followerList.FollowerCount += 1

		db.Model(&userAction).Where("uid = ?", user.Uid).Update("follow_list", followList.UserList)
		db.Model(&userAction).Where("uid = ?", user.Uid).Update("follow_count", followList.FollowCount)
		db.Model(&userAction).Where("uid = ?", id).Update("follower_list", followerList.UserList)
		db.Model(&userAction).Where("uid = ?", id).Update("follower_count", followerList.FollowerCount)

		ctx.JSON(http.StatusOK, models.Status{
			StatusCode: 0,
			StatusMsg:  FocusSuccess,
		})
	} else if flag == 2 {
		userAction.IsFollow = false

		followList.UserList = strings.Replace(followList.UserList, "/"+id+"/", "/", 1)
		followerList.UserList = strings.Replace(followerList.UserList, "/"+strconv.FormatInt(user.Uid, 10)+"/", "/", 1)
		followList.FollowCount -= 1
		followerList.FollowerCount -= 1

		db.Model(&userAction).Where("uid = ?", user.Uid).Update("follow_list", followList.UserList)
		db.Model(&userAction).Where("uid = ?", user.Uid).Update("follow_count", followList.FollowCount)
		db.Model(&userAction).Where("uid = ?", id).Update("follower_list", followerList.UserList)
		db.Model(&userAction).Where("uid = ?", id).Update("follower_count", followerList.FollowerCount)

		ctx.JSON(http.StatusOK, models.Status{
			StatusCode: 0,
			StatusMsg:  UnfollowSuccess,
		})
	}
}
